package com.ty.tour.common.utils;

/**
 * @Description: 自定义响应数据结构
 * 统一规范 code
 * 200：表示请求成功
 * 500：表示错误（未抛出异常），错误信息在msg字段中
 * 501：bean验证错误，不管多少个错误都以map形式返回
 * 502：拦截器拦截到用户登录信息出错
 * 555：异常抛出信息
 * 206：SQL语句执行失败 例如 往数据库表中添加完全一样的数据就会报错
 * <p>
 * 404：未找到，不存在，NULL
 * 408：繁忙
 * 205：逻辑错误
 */
public class JSONResult {

    // 响应业务状态
    private Integer status;

    // 响应消息
    private String message;

    private Integer total;

    public Integer getTotal() {
        return total;
    }

    public void setTotal(Integer total) {
        this.total = total;
    }

    // 响应中的数据
    private Object data;

    public static JSONResult build(Integer status, String msg, Object data) {

        return new JSONResult(status, msg, data);
    }
    public static JSONResult build(Integer status, String msg, Object data,Integer total) {
        return new JSONResult(status, msg, data,total);
    }

    public static JSONResult ok(Object data) {
        return new JSONResult(data);
    }

    public static JSONResult ok() {
        return new JSONResult(null);
    }

    public static JSONResult errorMsg(String msg) {
        return new JSONResult(500, msg, null);
    }

    public static JSONResult errorMap(Object data) {
        return new JSONResult(501, "error", data);
    }

    public static JSONResult errorTokenMsg(String msg) {
        return new JSONResult(502, msg, null);
    }

    public static JSONResult errorException(String msg) {
        return new JSONResult(555, msg, null);
    }

    public static JSONResult sqlError(String msg) {
        return new JSONResult(206, msg, null);
    }

    public JSONResult() {

    }

    public JSONResult(Object data) {
        this.status = 200;
        this.message = "OK";
        this.data = data;
    }

    public JSONResult(Integer status, String msg, Object data) {
        this.status = status;
        this.message = msg;
        this.data = data;
    }
    public JSONResult(Integer status, String msg, Object data,Integer total) {
        this.status = status;
        this.message = msg;
        this.data = data;
        this.total=total;
    }

    public Boolean isOK() {
        return this.status == 200;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String msg) {
        this.message = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }
}
